package com.jwdf.uda.ui.fragment.search;

import android.os.Bundle;
import android.text.TextUtils;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.event.InvitationEvent;
import com.jwdf.uda.event.MessageNoticeEvent;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.HttpHelper;
import com.jwdf.uda.http.action.InviteUserListAction;
import com.jwdf.uda.ui.adapter.InvitationAdapter;
import com.jwdf.uda.ui.adapter.item.UserSimpleItem;
import com.jwdf.uda.ui.fragment.base.SimpleListFragment;
import com.jwdf.uda.utils.EventBusUtil;

import retrofit2.Response;

/**
 * Created by Administrator on 2016/5/20.
 */
public class SearchInvitationResultsFragment extends SimpleListFragment {
    private String mContent;
    private PbBean.PbSignInviteUserListType type;
    private int questionId;

    private InvitationAdapter mAdapter;

    public static SearchInvitationResultsFragment instance(String content, PbBean.PbSignInviteUserListType type, int questionId) {
        SearchInvitationResultsFragment view = new SearchInvitationResultsFragment();
        Bundle args = new Bundle();
        args.putString("content", content);
        args.putSerializable("type", type);
        args.putInt("questionId", questionId);
        view.setArguments(args);
        return view;
    }

    public static SearchInvitationResultsFragment instance(PbBean.PbSignInviteUserListType type, int questionId) {
        SearchInvitationResultsFragment view = new SearchInvitationResultsFragment();
        Bundle args = new Bundle();
        args.putSerializable("type", type);
        args.putInt("questionId", questionId);
        view.setArguments(args);
        return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBusUtil.registerEvent(this);
        mContent = getArguments().getString("content", "");
        type = (PbBean.PbSignInviteUserListType) getArguments().getSerializable("type");
        questionId = getArguments().getInt("questionId");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBusUtil.unregisterEvent(this);
    }

    @Override
    protected boolean canPullRefresh() {
        return true;
    }

    @Override
    protected void initViews() {
        mAdapter = new InvitationAdapter(getContext());
        mAdapter.setCurrentUser(true);
        mDataBinding.commonFragmentList.setAdapter(mAdapter);
    }

    @Override
    protected void onListItemClick(int position) {
//        PbBean.PbUserSimple user = mAdapter.getItem(position).userSimple;
//        EventBusUtil.post(new GuideEvent(GuideEvent.EVENT_GUIDE_CHOOSE,user));
//        getActivity().finish();
    }

    @Override
    protected void requestData(final boolean refresh) {
        if (refresh && mAdapter.isEmpty()) {
            showProgressDialog();
        }
        int offset = refresh ? 0 : mAdapter.getCount();
        if (!mContent.isEmpty()) {
            type = PbBean.PbSignInviteUserListType.INVITE_USER_LIST_TYPE_SEARCH;
        }
        onNewRequestCall(InviteUserListAction.newInstance(getContext(), questionId, type, mContent, offset)
                .request(new AHttpService.IResCallback<PbBean.PbResQuestionInviteUserList>() {
                    @Override
                    public void onCallback(int resultCode, Response<PbBean.PbResQuestionInviteUserList> response, PbBean.PbBaseServerInfo
                            baseServerInfo) {
                        if (getView() != null) {
                            closeProgressDialog();
                            if (HttpHelper.showError(getContext(), resultCode, baseServerInfo, getString(R.string.error_net))) {
                                mDataBinding.commonFragmentList.stopRefresh();
                                mDataBinding.commonFragmentList.stopLoadMore();
                                return;
                            }
                            doSuccessResponse(refresh, response.body());
                        }
                    }
                }));
    }

    private void refreshUserInfo() {
        requestData(true);
        EventBusUtil.post(new MessageNoticeEvent(MessageNoticeEvent.REFRESH_USER_INFO));
    }

    private void doSuccessResponse(boolean refresh, PbBean.PbResQuestionInviteUserList body) {
        if (body != null) {
            if (body.getUserListList().isEmpty()) {
                mDataBinding.tvEmptyData.setText("对不起，未找到符合条件的用户");
                mDataBinding.commonFragmentList.setEmptyView(mDataBinding.tvEmptyData);
            } else {
                if (refresh) {
                    mAdapter.setData(UserSimpleItem.convert(body.getUserListList()));
                } else {
                    mAdapter.append(UserSimpleItem.convert(body.getUserListList()));
                }
            }
            if (PbBean.PbSignInviteUserListType.INVITE_USER_LIST_TYPE_FOLLOW == type ||
                    PbBean.PbSignInviteUserListType.INVITE_USER_LIST_TYPE_SEARCH == type) {
                mDataBinding.commonFragmentList.setPullLoadEnable(body.getUserListList().size() == getPageLength());
            }
            mDataBinding.commonFragmentList.stopRefresh();
            mDataBinding.commonFragmentList.stopLoadMore();
        }
    }

    public void onEvent(InvitationEvent invitationEvent) {
        switch (invitationEvent.getEvent()) {
            case InvitationEvent.INVITATION_ADD:
                for (int i = 0; i < mAdapter.getCount(); i++) {
                    UserSimpleItem item = mAdapter.getItem(i);
                    if (TextUtils.equals(invitationEvent.getUserSimple().getUserId(), item.userSimple.getUserId())) {
                        item.isInvite = true;
                        break;
                    }
                }
                mAdapter.notifyDataSetChanged();
                break;
        }
    }
}
